package com.example.trb.controller.form;

import com.example.trb.dto.ObjectDTO;
import com.example.trb.entity.ReviewForm;
import com.example.trb.entity.ReviewMeeting;
import com.example.trb.entity.Specialty;
import com.example.trb.entity.User;
import com.example.trb.service.review.ReviewFormService;
import com.example.trb.util.result.CommonResult;
import com.example.trb.util.result.ReturnCode;
import com.example.trb.util.result.ReviewFormCode;
import com.example.trb.vo.InitialFormVO;
import com.example.trb.vo.ReviewFormVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class ReviewFormController {
    @Autowired
    ReviewFormService rfService;
    @Value("${session_user}")
    String userKey;

    @GetMapping("/specialty")
    public CommonResult getSpecialty(@RequestParam("series") String series) {
        List<Specialty> result = rfService.listSpecialty(series);
        return CommonResult.success(result);
    }

    @PostMapping("/reviewform/commit")
    public CommonResult commitReviewForm(@RequestBody ReviewForm data) {
        rfService.updateStatusById(data.getId(), "待审核");
        return CommonResult.success(null);
    }

    @DeleteMapping("/reviewform")
    public CommonResult deleteReviewForm(@RequestBody ReviewForm data) {
        rfService.deleteById(data.getId());
        return CommonResult.success(null);
    }

    @PutMapping("/reviewform")
    public CommonResult updateReviewForm(@RequestBody @Validated ReviewForm data) {
        rfService.updateById(data);
        return CommonResult.success(null);
    }

    @GetMapping("/declarationseries")
    public CommonResult getAllDecSeries() {
        List<String> result = rfService.listDecSeries();
        return CommonResult.success(result);
    }

    @GetMapping("/reviewmeeting")
    public CommonResult getReviewMeeting(
            @RequestParam(value = "level", defaultValue = "") String level,
            @RequestParam(value = "series", defaultValue = "") String series
    ) {
        List<ReviewMeeting> result = rfService.listReviewMeeting(level, series);
        return CommonResult.success(result);
    }

    @PostMapping("/reviewform/init")
    public CommonResult initialForm(@RequestBody @Validated InitialFormVO vo, HttpSession session) {
        User user = (User) session.getAttribute(userKey);
        ObjectDTO dto = rfService.initialForm(vo, user.getId());
        ReturnCode returnCode = dto.getCode();

        if (returnCode == ReviewFormCode.NOW_YEAR_HAS_FORM) {
            Integer code = returnCode.code();
            String message = returnCode.message();
            return CommonResult.fail(code, message);
        }

        return CommonResult.success(dto.getData());
    }

    @GetMapping("/reviewform/list")
    public CommonResult listForm(HttpSession session) {
        User user = (User) session.getAttribute(userKey);
        ObjectDTO dto = rfService.listReviewForm(user.getId());
        return CommonResult.success(dto.getData());
    }

    @GetMapping("/reviewform")
    public CommonResult getReviewForm(@RequestParam("id") Integer id) {
        ReviewFormVO vo = rfService.getReviewFormById(id);
        return CommonResult.success(vo);
    }
}
